﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaLibraryManager.Data;

namespace MediaLibraryManager.Win
{
	public partial class VideoLibraryEditor : UserControl,IMediaEditor
	{
		private MediaLibraryApplicationContext appContext;

		public VideoLibraryEditor()
		{
			InitializeComponent();
		}

		private MediaLibraryApplicationContext AppContext
		{
			get
			{
				if (this.appContext == null)
					this.appContext = new MediaLibraryApplicationContext(
						SourceApplication.MediaLibraryManager,
						this.DesignMode);

				return this.appContext;
			}
		}

		private MediaLibraryDataContext DB
		{
			get
			{
				return this.AppContext.DB;
			}
		}

		private ObservableSongs observableSongs;
		private ObservableSongs ObservableSongs
		{
			get
			{
				if (this.observableSongs == null)
					this.observableSongs = new ObservableSongs(this.DB);

				return this.observableSongs;
			}
		}

		private ObservableEvents observableEvents;
		private ObservableEvents ObservableEvents
		{
			get
			{
				if (this.observableEvents == null)
					this.observableEvents = new ObservableEvents(this.DB);

				return this.observableEvents;
			}
		}

		private ObservableVersions observableVersion;
		private ObservableVersions ObservableVersions
		{
			get
			{
				if (this.observableVersion == null)
					this.observableVersion = new ObservableVersions(this.DB);

				return this.observableVersion;
			}
		}

		private void VideoLibraryEditor_Load(object sender, EventArgs e)
		{
			BindingSource bs = new BindingSource();
			bs.DataSource = this.DB.vVideos;
			grid.DataSource = bs;

			((DataGridViewComboBoxColumn)grid.Columns["MediaClassID"]).DataSource = this.DB.MediaClasses;
			((DataGridViewComboBoxColumn)grid.Columns["MediaClassID"]).ValueMember = "MediaClassID";
			((DataGridViewComboBoxColumn)grid.Columns["MediaClassID"]).DisplayMember = "Name";

			((DataGridViewComboBoxColumn)grid.Columns["SongID"]).DataSource = this.ObservableSongs;
			((DataGridViewComboBoxColumn)grid.Columns["SongID"]).ValueMember = "SongID";
			((DataGridViewComboBoxColumn)grid.Columns["SongID"]).DisplayMember = "Name";

			((DataGridViewComboBoxColumn)grid.Columns["EventID"]).DataSource = this.ObservableEvents;
			((DataGridViewComboBoxColumn)grid.Columns["EventID"]).ValueMember = "EventID";
			((DataGridViewComboBoxColumn)grid.Columns["EventID"]).DisplayMember = "Name";

			((DataGridViewComboBoxColumn)grid.Columns["VersionID"]).DataSource = this.ObservableVersions;
			((DataGridViewComboBoxColumn)grid.Columns["VersionID"]).ValueMember = "VersionID";
			((DataGridViewComboBoxColumn)grid.Columns["VersionID"]).DisplayMember = "Name";

			//grid.Columns["DateProduced"].colu
		}

		private void grid_RowEnter(object sender, DataGridViewCellEventArgs e)
		{
			thumbGen.Row = grid.Rows[e.RowIndex];
		}

		private void grid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.AutoCommit)
			{
				this.DB.SubmitChanges();
			}
		}

		private void grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if ((
					this.grid.Columns[this.grid.CurrentCell.ColumnIndex].Name == "SongID" ||
					this.grid.Columns[this.grid.CurrentCell.ColumnIndex].Name == "EventID" ||
					this.grid.Columns[this.grid.CurrentCell.ColumnIndex].Name == "VersionID"
				) &&
				e.Control is ComboBox)
			{
				((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
				((ComboBox)e.Control).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
				((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
			}
		}

		private void grid_CellLeave(object sender, DataGridViewCellEventArgs e)
		{
			if ((
					grid.Columns[e.ColumnIndex].Name == "SongID" ||
					grid.Columns[e.ColumnIndex].Name == "EventID" ||
					grid.Columns[e.ColumnIndex].Name == "VersionID"
				) &&
				this.grid.EditingControl is ComboBox)
			{
				ComboBox cb = (ComboBox)this.grid.EditingControl;
				cb.SelectedIndex = cb.FindStringExact(cb.Text);

				if (cb.SelectedIndex == -1)
				{
					string text = cb.Text;

					if (grid.Columns[e.ColumnIndex].Name == "SongID")
					{
						Song s = new Song();
						s.SongID = Guid.NewGuid();
						s.Name = cb.Text;

						this.DB.Songs.InsertOnSubmit(s);
						this.ObservableSongs.Add(s);

						//TODO: This may be done in other way so it doesn't flicker on the screen
						cb.Visible = false;
						cb.DataSource = null;
						cb.DataSource = this.ObservableSongs;
						cb.ValueMember = "SongID";
						cb.DisplayMember = "Name";
						cb.Visible = true;
					}
					else if (grid.Columns[e.ColumnIndex].Name == "EventID")
					{
						Event ev = new Event();
						ev.EventID = Guid.NewGuid();
						ev.Name = cb.Text;

						this.DB.Events.InsertOnSubmit(ev);
						this.ObservableEvents.Add(ev);

						//TODO: This may be done in other way so it doesn't flicker on the screen
						cb.Visible = false;
						cb.DataSource = null;
						cb.DataSource = this.ObservableEvents;
						cb.ValueMember = "EventID";
						cb.DisplayMember = "Name";
						cb.Visible = true;
					}
					else if (grid.Columns[e.ColumnIndex].Name == "VersionID")
					{
						Version ver = new Version();
						ver.VersionID = Guid.NewGuid();
						ver.Name = cb.Text;

						this.DB.Versions.InsertOnSubmit(ver);
						this.ObservableVersions.Add(ver);

						//TODO: This may be done in other way so it doesn't flicker on the screen
						cb.Visible = false;
						cb.DataSource = null;
						cb.DataSource = this.ObservableVersions;
						cb.ValueMember = "VersionID";
						cb.DisplayMember = "Name";
						cb.Visible = true;
					}

					cb.SelectedIndex = cb.FindStringExact(text);
				}
			}
		}

		private void grid_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			int a = 0;
		}

		#region IMediaEditor Members

		public bool AutoCommit { get; set; }

		public void CommitChanges()
		{
			this.DB.SubmitChanges();
		}

		#endregion
	}
}
